package com.bootdo.collection.utils;


import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion;
import io.lettuce.core.StatefulRedisConnectionImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.List;

@Component
public class JedisUtil {

    private static String host;


    private static int port;

    private static JedisPool jedisPool;

    @Value("${spring.redis.host}")
    public  void setHost(String host) {
        JedisUtil.host = host;
    }
    @Value("${spring.redis.port}")
    public  void setPort(int port) {
        JedisUtil.port = port;
    }

    public  static void init(){
        if(jedisPool == null ){
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setMaxTotal(5);
            jedisPool = new JedisPool(jedisPoolConfig,host,port);
        }
    }


   public static void lpush(String key,String value){
       Jedis jedis = null;
       try {
           jedis = jedisPool.getResource();
           jedis.lpush(key,value);
           jedis.expire(key,60 * 30);
       } catch (Exception e) {
           //释放redis对象
           e.printStackTrace();
       } finally {
           //返还到连接池
           jedis.close();
       }
   }






   public static String hset(String key,String field,String value){
       Jedis jedis = null;
       try {
           jedis = jedisPool.getResource();
           jedis.hset(key,field,value);
           jedis.expire(key, 60 * 60 * 8);
       } catch (Exception e) {
           //释放redis对象
           e.printStackTrace();
       } finally {
           //返还到连接池
           jedis.close();
       }
       return null;
   }

   public static void flush(String key){
       Jedis jedis = null;
       try {
           jedis = jedisPool.getResource();
           jedis.del(key);
       } catch (Exception e) {
           //释放redis对象
           e.printStackTrace();
       } finally {
           //返还到连接池
           jedis.close();
       }

   }

   public static String rpop(String  key){
       Jedis jedis = null;
       try {
           jedis = jedisPool.getResource();
           return jedis.lpop(key);
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           //返还到连接池
           jedis.close();
       }
       return null;
   }


   public static boolean exists(String key){
       Jedis jedis = null;
       try {
           jedis = jedisPool.getResource();
          return jedis.exists(key);
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           //返还到连接池
           jedis.close();
       }
       return false;
   }


   public static String get(String key){
       Jedis jedis = null;
       try {
           jedis = jedisPool.getResource();
           return jedis.get(key);
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           //返还到连接池
           jedis.close();
       }
       return null;
   }

   public static String hget(String key,String field){
       Jedis jedis = null;
       try {
           jedis = jedisPool.getResource();
           return jedis.hget(key,field);
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           //返还到连接池
           jedis.close();
       }
       return null;
   }

    public static String brpop(String key){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.brpop(0,key).get(1);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //返还到连接池
            jedis.close();
        }
        return null;
    }

    public static void set(String key,String value){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key,value);
            jedis.expire(key, 60 * 60 * 8);
        } catch (Exception e) {
            //释放redis对象
            e.printStackTrace();
        } finally {
            //返还到连接池
            jedis.close();
        }
    }




}

